package com.zxy.ziems.server.storage.task;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zxy.btp.redis.utils.RedisUtil;
import com.zxy.ziems.common.pojo.MeasureDataBean;
import com.zxy.ziems.server.alarm.service.AlarmRecordService;
import com.zxy.ziems.server.board.constant.BoardConstant;
import com.zxy.ziems.server.device.pojo.entity.DeviceEntity;
import com.zxy.ziems.server.device.service.DeviceService;
import com.zxy.ziems.server.storage.service.MeasureDataStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class GasCollectorTask {

    private String clientId;
    private String clientSecret;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private MeasureDataStorageService measureDataStorageService;

    @Autowired
    private AlarmRecordService alarmRecordService;

    public void gasTask() {

    }

    public void synGasData() {
        List<DeviceEntity> deviceEntityList = deviceService.listByTypeCode(BoardConstant.DeviceTypeCodeEnum.GAS.getTypeCode());
        List<MeasureDataBean> measureDataBeans = new ArrayList<>();
        for (DeviceEntity deviceEntity : deviceEntityList){

        }
        measureDataStorageService.storageMeasureDataList(measureDataBeans);
        alarmRecordService.storageAlarmByAlamRule(measureDataBeans);

    }

    public String getGasReadNum(){
        HttpRequest httpRequest = HttpUtil.createPost("");
        Map<String, String> headers = new HashMap<>();
        headers.put("token", "");
        headers.put("appid", "");
        headers.put("Content-Type", "application/json");
        headers.put("data-type", "JSON");
        headers.put("Authorization", "Bearer " + getToken());
        headers.put("api-version", "1");
        httpRequest.addHeaders(headers);
        Map<String, String> params = new HashMap<>();
        params.put("domain_id", "domain_id");
        params.put("meter_code", "meter_code");
        params.put("meter_code", "meter_code");
        httpRequest.body(JSONUtil.toJsonStr(params));
        String response = httpRequest.execute().body();
        JSONObject respObject = JSONUtil.parseObj(response);
        return respObject.getStr("read_Num");
    }


    public String getToken() {
        String accessKey = (String) redisUtil.get("GAS_ACCESS_KEY");
        if (StrUtil.isNotEmpty(accessKey)) {
            return accessKey;
        }
        String baseUrl = "";
        StringJoiner url = new StringJoiner("&", baseUrl + "?", "");
        url.add("client_id=" + clientId);
        url.add("client_secret=" + clientSecret);
        url.add("grant_type=client_credentials");
        url.add("longTokenFlag=Y");

        String response = HttpUtil.get(url.toString());

        JSONObject respObject = JSONUtil.parseObj(response);
        accessKey = respObject.getStr("access_token");
        String expiresIn = respObject.getStr("expires_in");
        redisUtil.set("GAS_ACCESS_KEY", accessKey, Long.valueOf(expiresIn));
        return accessKey;
    }
}
